﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SuporeSliplampApp.Base
{
    public class SocketManage
    {
        public static Socket clientSocket;
        public static string  ip_str=string.Empty;
        public static string ip_port = string.Empty;
        public static Thread threadReceive;

        public static void connect(string ipStr,string portStr)
        {
            IPAddress ip = IPAddress.Parse(ipStr);
            clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
             try
            {
                clientSocket.Connect(ip,Convert.ToInt32(portStr));
                threadReceive = new Thread(new ThreadStart(Receive));
                threadReceive.IsBackground = true;
                threadReceive.Start();

            }
            catch (Exception)
            {

                throw new Exception("服务器连接出错!");
            }
            
        }

        private static void Receive()
        {
            string res = string.Empty;
            try
            {
                while (true)
                {
                    byte[] buff = new byte[1703520];
                    int r = clientSocket.Receive(buff,buff.Length,SocketFlags.None);
                }
            }
            catch (Exception)
            {

                throw new Exception("获取数据出错！"); ;
            }
        }
    }
}
